package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.BatchQueryUsersUnionTestDataReq;
import com.sg.dto.base.framework.req.QueryUsersUnionDetailReq;
import com.sg.dto.base.framework.req.QueryUsersUnionListReq;
import com.sg.entity.UsersUnion;
import com.sg.service.base.framework.MUsersUnionService;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.UsersUnionMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MUsersUnionServiceImpl
        extends ServiceImpl<UsersUnionMapper, UsersUnion>
        implements MUsersUnionService {

    @Autowired
    UsersUnionMapper usersUnionMapper;

    /**
     * 1-2-03查询联盟用户详情[157]
     * gen by moon at 10/16/2022, 11:06:18 AM
     */
    @Trace(operationName = "1-2-03查询联盟用户详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public UsersUnion queryUsersUnionDetail(QueryUsersUnionDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<UsersUnion> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<UsersUnion> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                UsersUnion retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-03新增联盟用户[53]
     * gen by moon at 10/16/2022, 11:06:15 AM
     */
    @Trace(operationName = "1-2-03新增联盟用户")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addUsersUnion(UsersUnion usersUnion) {
        usersUnion.setActivateRecordId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        usersUnion.setActivetionTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        usersUnion.setIsValid("FALSE");
        usersUnion.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (super.save(usersUnion)) {
            return usersUnion.getActivateRecordId();
        } else {
            return "-1";
        }

    }

    /**
     * 1-2-03查询联盟用户列表[56]
     * gen by moon at 10/16/2022, 11:06:17 AM
     */
    @Trace(operationName = "1-2-03查询联盟用户列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<UsersUnion> queryUsersUnionList(QueryUsersUnionListReq reqDto) {

        try {
            QueryWrapper<UsersUnion> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(UsersUnion.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(UsersUnion.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(UsersUnion.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<UsersUnion>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-03删除联盟用户[55]
     * gen by moon at 10/16/2022, 11:06:16 AM
     */
    @Trace(operationName = "1-2-03删除联盟用户")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteUsersUnion(String activateRecordId) {

        return super.removeById(activateRecordId);
    }

    /**
     * 1-2-03修改联盟用户[54]
     * gen by moon at 10/16/2022, 11:06:15 AM
     */
    @Trace(operationName = "1-2-03修改联盟用户")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateUsersUnion(UsersUnion usersUnion) {

        return super.updateById(usersUnion);
    }

    /**
     * M1批量查询联盟用户测试[4886]
     * gen by moon at 10/16/2022, 11:06:19 AM
     */
    @Trace(operationName = "M1批量查询联盟用户测试")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<UsersUnion> batchQueryUsersUnionTestData(BatchQueryUsersUnionTestDataReq reqDto) {

        try {
            QueryWrapper<UsersUnion> queryWrapper = new QueryWrapper<>();

            if (reqDto.getUsersUnionTestDataList() == null || reqDto.getUsersUnionTestDataList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("user_id", reqDto.getUsersUnionTestDataList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(UsersUnion.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(UsersUnion.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(UsersUnion.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<UsersUnion>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M1批量删联盟用户测试[4887]
     * gen by moon at 10/16/2022, 11:06:19 AM
     */
    @Trace(operationName = "M1批量删联盟用户测试")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteUsersUnionTestData(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }
}
